<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入标题"/>
      <el-input v-model="listQuery.content" clearable class="filter-item" style="width: 200px;" placeholder="请输入内容"/>
      <el-button v-permission="['GET /admin/humanity/list']" class="filter-item" type="primary" icon="el-icon-search"
                 @click="getList(true)">查找
      </el-button>
      <el-button v-permission="['POST /admin/humanity/create']" class="filter-item" type="primary" icon="el-icon-edit"
                 @click="handleCreate">添加
      </el-button>
    </div>
    <!-- 查询结果 -->
    <el-table v-loading="list.length < 1" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column width="60" align="center" label="ID" prop="id"/>
      <el-table-column align="center" label="标题" prop="title" :show-overflow-tooltip="true"/>
      <el-table-column align="center" label="描述" prop="detail" :show-overflow-tooltip="true"/>
      <el-table-column align="center" label="图片地址" prop="link" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <img v-if="scope.row.link" :src="scope.row.link && scope.row.link.split(';')[0]" width="80" height="60">
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="addTime" sortable/>
      <el-table-column align="center" label="更新时间" prop="updateTime" sortable/>
      <el-table-column width="60" align="center" label="转发次数" prop="forwardCount"/>
      <el-table-column align="center" label="类型" prop="graphicType">
        <template slot-scope="scope">
          <el-tag type="success">{{ types[scope.row.graphicType] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['POST /admin/humanity/update']" type="primary" size="mini"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-permission="['POST /admin/humanity/delete']" type="danger" size="mini"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="dataForm && (dataForm.id ? '编辑' : '创建')" :visible.sync="isDiglog" width="70%">
      <add-form v-if="isDiglog" :dataForm="dataForm" :cancel="cancel"></add-form>
    </el-dialog>
  </div>
</template>

<style>
</style>

<script>
import { list, remove } from '@/api/apiHumanity'
import AddForm from './add'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Humanity',
  components: { Pagination, AddForm},
  data() {
    return {
      types: ['旅行之选','风情民宿','媒体信息'],
      dataForm: undefined,
      list: [],
      total: 0,
      isDiglog: false,
      listQuery: {
        page: 1,
        limit: 20,
        title: undefined,
        content: undefined,
        sort: 'add_time',
        order: 'desc'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    cancel(){
      this.isDiglog = false
      this.getList(true)
    },
    getList(init = false) {
      if(init){
        this.listQuery.page = 1
      }
      this.list = []
      this.total = 0
      list(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
        })
    },
    handleCreate() {
      this.dataForm = {
        graphicType: 0,
        link: []
      }
      this.isDiglog = true
    },
    handleUpdate(row) {
      const dataT = JSON.parse(JSON.stringify(row))
      let links = []
      dataT.link.split(';').forEach(link => {
        links.push({
          url: link
        })
      })
      dataT.link = links
      this.dataForm = dataT
      this.isDiglog = true
    },
    handleDelete(row) {
      remove({ ids: row.id }).then(response => {
        this.$notify.success({
          title: '成功',
          message: '删除成功'
        })
        const index = this.list.indexOf(row)
        this.list.splice(index, 1)
      })
    }
  }
}
</script>
